
public class NPC extends Ship {
   
    final static byte MOVE_FREE_MOVE  = 0;
    final static byte MOVE_TURN_LEFT  = 1;
    final static byte MOVE_TURN_RIGHT = 2;
    final static byte MOVE_BREAK      = 3;
    final static byte MOVE_STOP       = 4;
    final static byte MOVE_FINISHED   = 5;

    final static byte MIN_DISTANCE      = 30;
    final static byte CRITICAL_DISTANCE = 5;

    public byte npcMoveState, npcBehavior,
                wpDirection;
    public int curWay, curWayPoint, nextWayPoint, offsetWayPoint, 
               nwpPosX, nwpPosY, // position of next waypoint
               radius;
    private boolean isTargetSet;

    NPC(byte type, int npcIndex) {
        super(Constants.SHIP_NPC, type, GameScreen.wayPoints[npcIndex][0].x, Constants.SL_COUNTTILES * Constants.TILE_HEIGHT);
        curWay = npcIndex;
    }

/**************************************************************************************************/

    public void init() {
        super.init(startPosX, startPosY);
        racingTime = 0;
      
        curWayPoint = 0;
        nextWayPoint = curWayPoint + 1;

        isTargetSet = false;

        npcGetNextWayPoint();

        npcGetDirection();

        npcMoveState = MOVE_STOP;
        npcBehavior = GameScreen.npcBehavior[curWay];
    }

/**************************************************************************************************/

    public void npcStartRacing() {
        shipState = Constants.SHIP_STATE_MOVE;
        aBody.startAnimation(Constants.SHIP_ANIMATIONS[type][bodyState], Constants.ANIMATION_PLAY_REPEAT);
    }

/**************************************************************************************************/

    private void npcGetNextWayPoint() {
        nwpPosX = GameScreen.wayPoints[curWay][nextWayPoint].x; 
        nwpPosY = GameScreen.wayPoints[curWay][nextWayPoint].y;
    }

/**************************************************************************************************/

    private void npcGetDirection() {
        if (nwpPosX > rightPosX) {
            wpDirection = Constants.DIRECTION_RIGHT;
        }
        else if (nwpPosX < leftPosX) {
            wpDirection = Constants.DIRECTION_LEFT;
        }
        else {
            wpDirection = Constants.DIRECTION_STRAIGHT;
        }
    }

/**************************************************************************************************/
   
    public void hitWeapon(byte type) {
        if (itemType == Constants.ITEM_POWERUP && powerupType == Constants.SPECIALITEM_SHIELD) activatePowerUp();
        changeHealth(Constants.WEAPON_DAMAGE[type]);
        if (npcBehavior == Constants.NPC_INDIFFERENT) npcBehavior = Constants.NPC_AGRESSIVE;
    }

/**************************************************************************************************/
   
    public void npcUpdate(long dt) {
        long pointDir;
        int dAngle;
        boolean flag;

        move(dt);

        npcGetDirection();

        switch (npcBehavior) {
            case Constants.NPC_AGRESSIVE:
                switch(locationAlienShip) {
                    case Constants.LOCATION_TOP_CENTER:
                        if (isFrontTargetLocked && itemType == Constants.ITEM_WEAPON && (weaponType == Constants.WEAPON_LASER || weaponType == Constants.WEAPON_ROCKET) && shooter.isReady()) GameScreen.addShot(this, weaponType);
                    break;
                    
                    case Constants.LOCATION_BOTTOM_CENTER:
                        if(isRearTargetLocked && itemType == Constants.ITEM_WEAPON && (weaponType == Constants.WEAPON_BOMB || weaponType == Constants.WEAPON_MINE) && shooter.isReady()) GameScreen.addShot(this, weaponType);
                    break;
                }
            break;
        }

        if (wasShipHit) {
            if (npcBehavior == Constants.NPC_INDIFFERENT) npcBehavior = Constants.NPC_AGRESSIVE;
            wasShipHit = false;
        }
        if (health < Constants.SHIP_CRITICAL_HEALTH) npcBehavior = Constants.NPC_WARY;
        else if (npcBehavior == Constants.NPC_WARY) npcBehavior = Constants.NPC_INDIFFERENT;

        switch (shipState) {
            case Constants.SHIP_STATE_MOVE:
                switch (obstacleType) {
                /*** OBSTACLE_NONE ********************************************************************************/
                    case Constants.OBSTACLE_NONE:
                  
                        if (locationAlienShip == Constants.LOCATION_NONE) {
                            switch (npcMoveState) {
                                case MOVE_STOP:
                                    increaseMainEnginePower();
                                    npcMoveState = MOVE_FREE_MOVE;
                                break;
                                
                                case MOVE_FREE_MOVE:
                                    if ((wpDirection == Constants.DIRECTION_LEFT) && (leftDistance > MIN_DISTANCE)) {
                                        startLeftEngine();
                                        npcMoveState = MOVE_TURN_LEFT;
                                    }
                                    else if ((wpDirection == Constants.DIRECTION_RIGHT) && (rightDistance > MIN_DISTANCE)) {
                                        startRightEngine();
                                        npcMoveState = MOVE_TURN_RIGHT;
                                    }
                                    else {
                                        increaseMainEnginePower();
                                        if ((wpDirection == Constants.DIRECTION_STRAIGHT) && 
                                            ((nwpPosY - posY) > Constants.NPC_NITRO_DISCTANCE) && 
                                            (itemType == Constants.ITEM_POWERUP) && 
                                            (powerupType == Constants.SPECIALITEM_NITRO)) {
                                                activatePowerUp();
                                            }
                                         }
                                break;
                                
                                case MOVE_TURN_LEFT:
                                    if ((wpDirection == Constants.DIRECTION_STRAIGHT) || 
                                        (wpDirection == Constants.DIRECTION_RIGHT) ||
                                        ((wpDirection == Constants.DIRECTION_LEFT) && (leftDistance < MIN_DISTANCE))) {
                                        stopTurnEngine(Constants.SHIP_TURNSTATE_FREE);
                                        npcMoveState = MOVE_FREE_MOVE;
                                    }
                                    else {
                                        increaseMainEnginePower();
                                    }
                                break;
                                
                                case MOVE_TURN_RIGHT:
                                    if ((wpDirection == Constants.DIRECTION_STRAIGHT) || 
                                        (wpDirection == Constants.DIRECTION_LEFT) ||
                                        ((wpDirection == Constants.DIRECTION_RIGHT) && (rightDistance < MIN_DISTANCE))) {
                                        stopTurnEngine(Constants.SHIP_TURNSTATE_FREE);
                                        npcMoveState = MOVE_FREE_MOVE;
                                    }
                                    else {
                                        increaseMainEnginePower();
                                    }
                                break;

                                case MOVE_BREAK:
                                    stopBrakeEngine();
                                    npcMoveState = MOVE_FREE_MOVE;
                                break;
                            }
                        }
                        else {
                            switch (npcBehavior) {
                                /*** NPC_INDIFFERENT ******************************************************/
                                case Constants.NPC_INDIFFERENT:
                                    switch (locationAlienShip) {
                                        /*** LOCATION_TOP_LEFT **********************************************/
                                        case Constants.LOCATION_TOP_LEFT:
                                        case Constants.LOCATION_TOP_RIGHT:
                                        case Constants.LOCATION_MIDDLE_LEFT:
                                        case Constants.LOCATION_MIDDLE_RIGHT:
                                        case Constants.LOCATION_BOTTOM_LEFT:
                                        case Constants.LOCATION_BOTTOM_RIGHT:
                                        case Constants.LOCATION_BOTTOM_CENTER:
                                            switch (npcMoveState) {
                                                /*** MOVE_STOP *********************************************/
                                                case MOVE_STOP:
                                                    increaseMainEnginePower();
                                                    npcMoveState = MOVE_FREE_MOVE;
                                                break;
                                                
                                                /*** MOVE_FREE_MOVE ****************************************/
                                                case MOVE_FREE_MOVE:
                                                    if ((wpDirection == Constants.DIRECTION_LEFT) && 
                                                        (leftDistance > MIN_DISTANCE) && 
                                                        (locationAlienShip != Constants.LOCATION_MIDDLE_LEFT) &&
                                                        (locationAlienShip != Constants.LOCATION_TOP_LEFT)) {
                                                        startLeftEngine();
                                                        npcMoveState = MOVE_TURN_LEFT;
                                                    }
                                                    else if ((wpDirection == Constants.DIRECTION_RIGHT) && 
                                                             (rightDistance > MIN_DISTANCE) &&
                                                             (locationAlienShip != Constants.LOCATION_MIDDLE_RIGHT) &&
                                                             (locationAlienShip != Constants.LOCATION_TOP_RIGHT)) {
                                                        startRightEngine();
                                                        npcMoveState = MOVE_TURN_RIGHT;
                                                    }
                                                    else {
                                                        increaseMainEnginePower();
                                                    }
                                                break;
                                                
                                                /*** MOVE_TURN_LEFT ****************************************/
                                                case MOVE_TURN_LEFT:
                                                    if ((wpDirection == Constants.DIRECTION_STRAIGHT) || (wpDirection == Constants.DIRECTION_RIGHT)) {
                                                        stopTurnEngine(Constants.SHIP_TURNSTATE_FREE);
                                                        npcMoveState = MOVE_FREE_MOVE;
                                                    }
                                                    else if ((wpDirection == Constants.DIRECTION_LEFT) && ((leftDistance < MIN_DISTANCE) || (locationAlienShip == Constants.LOCATION_MIDDLE_LEFT) || (locationAlienShip == Constants.LOCATION_TOP_LEFT))) {
                                                        stopTurnEngine(Constants.SHIP_TURNSTATE_FREE);
                                                        npcMoveState = MOVE_FREE_MOVE;
                                                    }
                                                    else {
                                                        increaseMainEnginePower();
                                                    }
                                                break;
                                                
                                                /*** MOVE_TURN_RIGHT ***************************************/
                                                case MOVE_TURN_RIGHT:
                                                    if ((wpDirection == Constants.DIRECTION_STRAIGHT) || (wpDirection == Constants.DIRECTION_LEFT)) {
                                                        stopTurnEngine(Constants.SHIP_TURNSTATE_FREE);
                                                        npcMoveState = MOVE_FREE_MOVE;
                                                    }
                                                    else if ((wpDirection == Constants.DIRECTION_RIGHT) && ((rightDistance < MIN_DISTANCE) || (locationAlienShip == Constants.LOCATION_MIDDLE_RIGHT) || (locationAlienShip == Constants.LOCATION_TOP_RIGHT))) {
                                                        stopTurnEngine(Constants.SHIP_TURNSTATE_FREE);
                                                        npcMoveState = MOVE_FREE_MOVE;
                                                    }
                                                    else {
                                                        increaseMainEnginePower();
                                                    }
                                                break;
                                            }
                                        break;
                                        
                                        /*** LOCATION_TOP_CENTER ********************************************/
                                        case Constants.LOCATION_TOP_CENTER:
                                            switch (npcMoveState) {
                                            
                                                /*** MOVE_FREE_MOVE ****************************************/
                                                case MOVE_FREE_MOVE:
                                                    if (leftDistance > MIN_DISTANCE) {
                                                        startLeftEngine();
                                                        npcMoveState = MOVE_TURN_LEFT;
                                                    }
                                                    else if (rightDistance > MIN_DISTANCE) {
                                                        startRightEngine();
                                                        npcMoveState = MOVE_TURN_RIGHT;
                                                    }
                                                    else {
                                                        stopTurnEngine(Constants.SHIP_TURNSTATE_FREE);
                                                        startBrakeEngine();
                                                        npcMoveState = MOVE_STOP;
                                                    }
                                                break;
                                                
                                                /*** MOVE_TURN_LEFT ****************************************/
                                                case MOVE_TURN_LEFT:
                                                    if (frontShipDistance < CRITICAL_DISTANCE) {
                                                        /*!!! Switch to ATTACK MODE !!!*/
                                                        npcBehavior = Constants.NPC_AGRESSIVE;
                                                        stopTurnEngine(Constants.SHIP_TURNSTATE_FREE);
                                                        npcMoveState = MOVE_FREE_MOVE;
                                                    }
                                                    else if(leftDistance < MIN_DISTANCE) {
                                                        startRightEngine();
                                                        npcMoveState = MOVE_TURN_RIGHT;
                                                    }
                                                    else {
                                                        increaseMainEnginePower();
                                                   }
                                                break;
                                                
                                                /*** MOVE_TURN_RIGHT ***************************************/
                                                case MOVE_TURN_RIGHT:
                                                    if (frontShipDistance < CRITICAL_DISTANCE) {
                                                        /*!!! Switch to ATTACK MODE !!!*/
                                                        npcBehavior = Constants.NPC_AGRESSIVE;
                                                        stopTurnEngine(Constants.SHIP_TURNSTATE_FREE);
                                                        npcMoveState = MOVE_FREE_MOVE;
                                                    }
                                                    else if (rightDistance < MIN_DISTANCE) {
                                                        startLeftEngine();
                                                        npcMoveState = MOVE_TURN_LEFT;
                                                    }
                                                    else {
                                                        increaseMainEnginePower();
                                                    }
                                                break;
                                            }
                                        break;
                                    }
                                break;
                              
                                /*** NPC_AGRESSIVE ********************************************************/
                                case Constants.NPC_AGRESSIVE:
                                    switch(locationAlienShip){
                                    /*** LOCATION_TOP_LEFT **********************************************/
                                        case Constants.LOCATION_TOP_LEFT:
                                            switch (npcMoveState) {
                                                /*** MOVE_STOP *********************************************/
                                                case MOVE_STOP:
                                                    if (frontShipDistance > (Constants.SHIP_RADAR_DISTANCE >> 1)) {
                                                        increaseMainEnginePower();
                                                        npcMoveState = MOVE_FREE_MOVE;
                                                    }
                                                break;
                                                
                                                /*** MOVE_FREE_MOVE & MOVE_TURN_RIGHT **********************/
                                                case MOVE_FREE_MOVE:
                                                case MOVE_TURN_RIGHT:
                                                    if (itemType == Constants.ITEM_WEAPON && 
                                                        (weaponType == Constants.WEAPON_LASER || weaponType == Constants.WEAPON_ROCKET) && 
                                                        leftDistance > MIN_DISTANCE && 
                                                        frontShipDistance > MIN_DISTANCE) {
                                                        startLeftEngine();
                                                        npcMoveState = MOVE_TURN_LEFT;
                                                    }
                                                    else {
                                                        switch (npcMoveState) {
                                                            case MOVE_FREE_MOVE:
                                                                stopTurnEngine(Constants.SHIP_TURNSTATE_FREE);
                                                            break;
                                                            
                                                            case MOVE_TURN_RIGHT:
                                                                startRightEngine();
                                                            break;
                                                        }
                                                        increaseMainEnginePower();
                                                    }
                                                break;
                                                
                                                /*** MOVE_TURN_RIGHT ***************************************/
                                                case MOVE_TURN_LEFT:
                                                    if ((leftDistance < MIN_DISTANCE) || (frontShipDistance < MIN_DISTANCE)) {
                                                        stopTurnEngine(Constants.SHIP_TURNSTATE_FREE);
                                                        npcMoveState = MOVE_FREE_MOVE;
                                                    }
                                                    else {
                                                        startLeftEngine();
                                                        increaseMainEnginePower();
                                                    }
                                                break;
                                            }
                                        break;

                                        /*** LOCATION_TOP_CENTER ********************************************/
                                        case Constants.LOCATION_TOP_CENTER:
                                            switch (npcMoveState) {
                                                /*** MOVE_STOP *********************************************/
                                                case MOVE_STOP:
                                                    if (frontShipDistance > (Constants.SHIP_RADAR_DISTANCE >> 1)) {
                                                        increaseMainEnginePower();
                                                        npcMoveState = MOVE_FREE_MOVE;
                                                    }
                                                break;
                                                
                                                /*** MOVE_FREE_MOVE ****************************************/
                                                case MOVE_FREE_MOVE:
                                                    if (frontShipDistance > (Constants.SHIP_RADAR_DISTANCE >> 1)) {
                                                        increaseMainEnginePower();
                                                    }
                                                    else {
                                                        if (leftDistance > rightDistance && leftDistance > MIN_DISTANCE) {
                                                            startLeftEngine();
                                                            npcMoveState = MOVE_TURN_LEFT;
                                                        }
                                                        else if (rightDistance > MIN_DISTANCE) {
                                                            startRightEngine();
                                                            npcMoveState = MOVE_TURN_RIGHT;
                                                        }
                                                    }
                                                break;
                                                
                                                /*** MOVE_TURN_LEFT & MOVE_TURN_RIGHT **********************/
                                                case MOVE_TURN_LEFT:
                                                case MOVE_TURN_RIGHT:
                                                    if (frontShipDistance > (Constants.SHIP_RADAR_DISTANCE >> 1)) {
                                                        stopTurnEngine(Constants.SHIP_TURNSTATE_FREE);
                                                        npcMoveState = MOVE_FREE_MOVE;
                                                    }
                                                    else {
                                                        switch (npcMoveState) {
                                                            case MOVE_TURN_LEFT:
                                                                startLeftEngine();
                                                            break;
                                                            
                                                            case MOVE_TURN_RIGHT:
                                                                startRightEngine();
                                                            break;
                                                        }
                                                        increaseMainEnginePower();
                                                    }
                                                break;
                                            }
                                        break;

                                        /*** LOCATION_TOP_RIGHT *********************************************/
                                        case Constants.LOCATION_TOP_RIGHT:
                                            switch (npcMoveState) {
                                                /*** MOVE_STOP *********************************************/
                                                case MOVE_STOP:
                                                    if (frontShipDistance > (Constants.SHIP_RADAR_DISTANCE >> 1)) {
                                                        increaseMainEnginePower();
                                                        npcMoveState = MOVE_FREE_MOVE;
                                                    }
                                                break;
                                                
                                                /*** MOVE_FREE_MOVE & MOVE_TURN_LEFT ***********************/
                                                case MOVE_FREE_MOVE:
                                                case MOVE_TURN_LEFT:
                                                    if (itemType == Constants.ITEM_WEAPON && 
                                                        (weaponType == Constants.WEAPON_LASER || weaponType == Constants.WEAPON_ROCKET) && 
                                                        rightDistance > MIN_DISTANCE && 
                                                        frontShipDistance > MIN_DISTANCE) {
                                                        startRightEngine();
                                                        npcMoveState = MOVE_TURN_RIGHT;
                                                    }
                                                    else {
                                                        switch (npcMoveState) {
                                                            case MOVE_FREE_MOVE:
                                                                stopTurnEngine(Constants.SHIP_TURNSTATE_FREE);
                                                            break;
                                                            
                                                            case MOVE_TURN_LEFT:
                                                                startLeftEngine();
                                                            break;
                                                        }
                                                        increaseMainEnginePower();
                                                    }
                                                break;
                                                
                                                /*** MOVE_TURN_RIGHT ***************************************/
                                                case MOVE_TURN_RIGHT:
                                                    if ((rightDistance < MIN_DISTANCE) || (frontShipDistance < MIN_DISTANCE)) {
                                                        stopTurnEngine(Constants.SHIP_TURNSTATE_FREE);
                                                        npcMoveState = MOVE_FREE_MOVE;
                                                    }
                                                    else {
                                                        startRightEngine();
                                                        increaseMainEnginePower();
                                                    }
                                                break;
                                            }
                                        break;

                                        /*** LOCATION_MIDDLE_LEFT *******************************************/
                                        case Constants.LOCATION_MIDDLE_LEFT:
                                        case Constants.LOCATION_MIDDLE_RIGHT:
                                            switch (npcMoveState) {
                                                /*** MOVE_STOP *********************************************/
                                                case MOVE_STOP:
                                                    increaseMainEnginePower();
                                                    npcMoveState = MOVE_FREE_MOVE;
                                                break;
                                                
                                                /*** MOVE_FREE_MOVE ****************************************/
                                                case MOVE_FREE_MOVE:
                                                    stopTurnEngine(Constants.SHIP_TURNSTATE_FREE);
                                                    increaseMainEnginePower();
                                                    if (((nwpPosY - posY) > Constants.NPC_NITRO_DISCTANCE) && 
                                                        (itemType == Constants.ITEM_POWERUP) && 
                                                        (powerupType == Constants.SPECIALITEM_NITRO)) {
                                                        activatePowerUp();
                                                    }
                                                break;
                                                
                                                /*** MOVE_TURN_LEFT & MOVE_TURN_RIGHT **********************/
                                                case MOVE_TURN_LEFT:
                                                case MOVE_TURN_RIGHT:
                                                case MOVE_BREAK:
                                                    stopTurnEngine(Constants.SHIP_TURNSTATE_FREE);
                                                    npcMoveState = MOVE_FREE_MOVE;
                                                break;
                                            }
                                        break;

                                        /*** LOCATION_BOTTOM_LEFT *******************************************/
                                        case Constants.LOCATION_BOTTOM_LEFT:
                                            switch (npcMoveState) {
                                                /*** MOVE_STOP *********************************************/
                                                case MOVE_STOP:
                                                    increaseMainEnginePower();
                                                    npcMoveState = MOVE_FREE_MOVE;
                                                break;
                                             
                                                /*** MOVE_FREE_MOVE & MOVE_TURN_RIGHT **********************/
                                                case MOVE_FREE_MOVE:
                                                case MOVE_TURN_RIGHT:
                                                    if (itemType == Constants.ITEM_WEAPON && 
                                                        (weaponType == Constants.WEAPON_BOMB || weaponType == Constants.WEAPON_MINE) && 
                                                        leftDistance > MIN_DISTANCE && 
                                                        frontShipDistance > MIN_DISTANCE) {
                                                        startLeftEngine();
                                                        npcMoveState = MOVE_TURN_LEFT;
                                                    }
                                                    else {
                                                        switch (npcMoveState) {
                                                            case MOVE_FREE_MOVE:
                                                                stopTurnEngine(Constants.SHIP_TURNSTATE_FREE);
                                                            break;
                                                            
                                                            case MOVE_TURN_RIGHT:
                                                                startRightEngine();
                                                            break;
                                                        }
                                                        increaseMainEnginePower();
                                                    }
                                                break;
                                                
                                                /*** MOVE_TURN_RIGHT ***************************************/
                                                case MOVE_TURN_LEFT:
                                                    if (leftDistance < MIN_DISTANCE || dir != Constants.DIRECTION_LEFT) {
                                                        stopTurnEngine(Constants.SHIP_TURNSTATE_FREE);
                                                        npcMoveState = MOVE_FREE_MOVE;
                                                    }
                                                    else {
                                                        startLeftEngine();
                                                        increaseMainEnginePower();
                                                    }
                                                break;
                                            }
                                        break;

                                        /*** LOCATION_BOTTOM_CENTER *****************************************/
                                        case Constants.LOCATION_BOTTOM_CENTER:
                                            switch (npcMoveState) {
                                                /*** MOVE_STOP *********************************************/
                                                case MOVE_STOP:
                                                    increaseMainEnginePower();
                                                    npcMoveState = MOVE_FREE_MOVE;
                                                break;
                                                
                                                /*** MOVE_FREE_MOVE ****************************************/
                                                case MOVE_FREE_MOVE:
                                                    stopTurnEngine(Constants.SHIP_TURNSTATE_FREE);
                                                    increaseMainEnginePower();
                                                break;
                                                
                                                /*** MOVE_TURN_LEFT & MOVE_TURN_RIGHT **********************/
                                                case MOVE_TURN_LEFT:
                                                case MOVE_TURN_RIGHT:
                                                    stopTurnEngine(Constants.SHIP_TURNSTATE_FREE);
                                                    npcMoveState = MOVE_FREE_MOVE;
                                                break;
                                            }
                                        break;

                                        /*** LOCATION_BOTTOM_RIGHT ******************************************/
                                        case Constants.LOCATION_BOTTOM_RIGHT:
                                            switch (npcMoveState) {
                                                /*** MOVE_STOP *********************************************/
                                                case MOVE_STOP:
                                                    increaseMainEnginePower();
                                                    npcMoveState = MOVE_FREE_MOVE;
                                                break;
                                                
                                                /*** MOVE_FREE_MOVE & MOVE_TURN_LEFT ***********************/
                                                case MOVE_FREE_MOVE:
                                                case MOVE_TURN_LEFT:
                                                    if (itemType == Constants.ITEM_WEAPON && 
                                                        (weaponType == Constants.WEAPON_BOMB || weaponType == Constants.WEAPON_MINE) && 
                                                        rightDistance > MIN_DISTANCE && 
                                                        frontShipDistance > MIN_DISTANCE) {
                                                        startRightEngine();
                                                        npcMoveState = MOVE_TURN_RIGHT;
                                                    }
                                                    else {
                                                        switch (npcMoveState) {
                                                            case MOVE_FREE_MOVE:
                                                                stopTurnEngine(Constants.SHIP_TURNSTATE_FREE);
                                                            break;
                                                            
                                                            case MOVE_TURN_LEFT:
                                                                startLeftEngine();
                                                            break;
                                                        }
                                                        increaseMainEnginePower();
                                                    }
                                                break;
                                                
                                                /*** MOVE_TURN_RIGHT ***************************************/
                                                case MOVE_TURN_RIGHT:
                                                    if (rightDistance < MIN_DISTANCE || dir != Constants.DIRECTION_LEFT) {
                                                        stopTurnEngine(Constants.SHIP_TURNSTATE_FREE);
                                                        npcMoveState = MOVE_FREE_MOVE;
                                                    }
                                                    else {
                                                        startRightEngine();
                                                        increaseMainEnginePower();
                                                    }
                                                break;
                                            }
                                        break;
                                    }
                                break;
                              
                                /*** NPC_WARY *************************************************************/
                                case Constants.NPC_WARY:
                                    switch (locationAlienShip) {
                                    
                                        /*** LOCATION_TOP_LEFT **********************************************/
                                       case Constants.LOCATION_TOP_LEFT:
                                       case Constants.LOCATION_TOP_RIGHT:
                                       case Constants.LOCATION_MIDDLE_LEFT:
                                       case Constants.LOCATION_MIDDLE_RIGHT:
                                       case Constants.LOCATION_BOTTOM_LEFT:
                                       case Constants.LOCATION_BOTTOM_RIGHT:
                                            switch (npcMoveState) {
                                                /*** MOVE_STOP *********************************************/
                                                case MOVE_STOP:
                                                    increaseMainEnginePower();
                                                    npcMoveState = MOVE_FREE_MOVE;
                                                break;
                                                
                                                /*** MOVE_FREE_MOVE ****************************************/
                                                case MOVE_FREE_MOVE:
                                                    if ((wpDirection == Constants.DIRECTION_LEFT) && 
                                                        (leftDistance > MIN_DISTANCE) && 
                                                        (locationAlienShip != Constants.LOCATION_MIDDLE_LEFT) &&
                                                        (locationAlienShip != Constants.LOCATION_TOP_LEFT)) {
                                                        startLeftEngine();
                                                        npcMoveState = MOVE_TURN_LEFT;
                                                    }
                                                    else if ((wpDirection == Constants.DIRECTION_RIGHT) && 
                                                            (rightDistance > MIN_DISTANCE) &&
                                                            (locationAlienShip != Constants.LOCATION_MIDDLE_RIGHT) &&
                                                            (locationAlienShip != Constants.LOCATION_TOP_RIGHT)) {
                                                        startRightEngine();
                                                        npcMoveState = MOVE_TURN_RIGHT;
                                                    }
                                                    else {
                                                        increaseMainEnginePower();
                                                    }
                                                break;
                                                
                                                /*** MOVE_TURN_LEFT ****************************************/
                                                case MOVE_TURN_LEFT:
                                                    if ((wpDirection == Constants.DIRECTION_STRAIGHT) || (wpDirection == Constants.DIRECTION_RIGHT)) {
                                                        stopTurnEngine(Constants.SHIP_TURNSTATE_FREE);
                                                        npcMoveState = MOVE_FREE_MOVE;
                                                    }
                                                    else if ((wpDirection == Constants.DIRECTION_LEFT) && ((leftDistance < MIN_DISTANCE) || (locationAlienShip == Constants.LOCATION_MIDDLE_LEFT) || (locationAlienShip == Constants.LOCATION_TOP_LEFT))) {
                                                        stopTurnEngine(Constants.SHIP_TURNSTATE_FREE);
                                                        npcMoveState = MOVE_FREE_MOVE;
                                                    }
                                                    else {
                                                        increaseMainEnginePower();
                                                    }
                                                break;
                                                
                                                /*** MOVE_TURN_RIGHT ***************************************/
                                                case MOVE_TURN_RIGHT:
                                                    if ((wpDirection == Constants.DIRECTION_STRAIGHT) || (wpDirection == Constants.DIRECTION_LEFT)) {
                                                        stopTurnEngine(Constants.SHIP_TURNSTATE_FREE);
                                                        npcMoveState = MOVE_FREE_MOVE;
                                                    }
                                                    else if ((wpDirection == Constants.DIRECTION_RIGHT) && ((rightDistance < MIN_DISTANCE) || (locationAlienShip == Constants.LOCATION_MIDDLE_RIGHT) || (locationAlienShip == Constants.LOCATION_TOP_RIGHT))) {
                                                        stopTurnEngine(Constants.SHIP_TURNSTATE_FREE);
                                                        npcMoveState = MOVE_FREE_MOVE;
                                                    }
                                                    else {
                                                        increaseMainEnginePower();
                                                    }
                                                break;
                                            }
                                        break;
                                        
                                        /*** LOCATION_TOP_CENTER ********************************************/
                                        case Constants.LOCATION_TOP_CENTER:
                                            switch (npcMoveState) {
                                                /*** MOVE_FREE_MOVE ****************************************/
                                                case MOVE_FREE_MOVE:
                                                    if (leftDistance > MIN_DISTANCE) {
                                                        startLeftEngine();
                                                        npcMoveState = MOVE_TURN_LEFT;
                                                    }
                                                    else if (rightDistance > MIN_DISTANCE) {
                                                        startRightEngine();
                                                        npcMoveState = MOVE_TURN_RIGHT;
                                                    }
                                                    else {
                                                        stopTurnEngine(Constants.SHIP_TURNSTATE_FREE);
                                                        startBrakeEngine();
                                                        npcMoveState = MOVE_STOP;
                                                    }
                                                break;
                                                
                                                /*** MOVE_TURN_LEFT ****************************************/
                                                case MOVE_TURN_LEFT:
                                                    if (frontShipDistance < CRITICAL_DISTANCE) {
                                                        stopTurnEngine(Constants.SHIP_TURNSTATE_FREE);
                                                        startBrakeEngine();
                                                        npcMoveState = MOVE_STOP;
                                                    }
                                                    else if (leftDistance < MIN_DISTANCE) {
                                                        startRightEngine();
                                                        npcMoveState = MOVE_TURN_RIGHT;
                                                    }
                                                    else {
                                                        increaseMainEnginePower();
                                                    }
                                                break;
                                                
                                                /*** MOVE_TURN_RIGHT ***************************************/
                                                case MOVE_TURN_RIGHT:
                                                    if (frontShipDistance < CRITICAL_DISTANCE) {
                                                        stopTurnEngine(Constants.SHIP_TURNSTATE_FREE);
                                                        startBrakeEngine();
                                                        npcMoveState = MOVE_STOP;
                                                    }
                                                    else if (rightDistance < MIN_DISTANCE) {
                                                        startLeftEngine();
                                                        npcMoveState = MOVE_TURN_LEFT;
                                                    }
                                                    else {
                                                        increaseMainEnginePower();
                                                    }
                                                break;
                                            }
                                        break;
                                        
                                        /*** LOCATION_BOTTOM_CENTER *****************************************/
                                        case Constants.LOCATION_BOTTOM_CENTER:
                                            switch (npcMoveState) {
                                                case MOVE_FREE_MOVE:
                                                    if (leftDistance > rightDistance) {
                                                        startLeftEngine();
                                                        npcMoveState = MOVE_TURN_LEFT;
                                                    }
                                                    else {
                                                        startRightEngine();
                                                        npcMoveState = MOVE_TURN_RIGHT;
                                                    }
                                                break;
                                                   
                                                case MOVE_TURN_LEFT:
                                                    if (leftDistance < MIN_DISTANCE) {
                                                        startRightEngine();
                                                        npcMoveState = MOVE_TURN_RIGHT;
                                                    }
                                                break;
                                                   
                                                case MOVE_TURN_RIGHT:
                                                    if (rightDistance < MIN_DISTANCE) {
                                                        startLeftEngine();
                                                        npcMoveState = MOVE_TURN_LEFT;
                                                    }
                                                break;
                                            }
                                        break;
                                    }
                                break;
                            }
                        }
                    break;
                  
                    /*** OBSTACLE_EDGE ********************************************************************************/
                    case Constants.OBSTACLE_EDGE:
                        switch (npcMoveState) {
                            case MOVE_STOP:
                                switch (GameScreen.borders[borderInFrontIndex].side) {
                                    case 0:
                                        //if(locationAlienShip != Constants.LOCATION_MIDDLE_RIGHT)
                                        {
                                            increaseMainEnginePower();
                                            npcMoveState = MOVE_FREE_MOVE;
                                        }
                                    break;
                                    
                                    case 1:
                                        //if(locationAlienShip != Constants.LOCATION_MIDDLE_LEFT)
                                        {
                                            increaseMainEnginePower();
                                            npcMoveState = MOVE_FREE_MOVE;
                                        }
                                    break;
                                }
                            break;

                            case MOVE_FREE_MOVE:
                                if (borderInFrontIndex > -1 && borderInFrontIndex < GameScreen.borders.length) {
                                    switch(GameScreen.borders[borderInFrontIndex].side){
                                        case 0:
                                            //if(locationAlienShip != Constants.LOCATION_MIDDLE_RIGHT)
                                            //{
                                                if(rightDistance > CRITICAL_DISTANCE) {
                                                    startRightEngine(GameScreen.borders[borderInFrontIndex].angle);
                                                    npcMoveState = MOVE_TURN_RIGHT;
                                                }
                                            //}
                                            //else
                                            //{
                                            //   startBrakeEngine();
                                            //   npcMoveState = MOVE_STOP;
                                            //}
                                        break;
                                        
                                        case 1:
                                            //if(locationAlienShip != Constants.LOCATION_MIDDLE_LEFT)
                                            //{
                                                if(leftDistance > CRITICAL_DISTANCE) {
                                                    startLeftEngine(GameScreen.borders[borderInFrontIndex].angle);
                                                    npcMoveState = MOVE_TURN_LEFT;
                                                }
                                            //}
                                            //else
                                            //{
                                            //   startBrakeEngine();
                                            //   npcMoveState = MOVE_STOP;
                                            //}
                                        break;
                                    }
                                }
                            break;

                            case MOVE_TURN_LEFT:
                                if ((borderInFrontIndex > -1 && borderInFrontIndex < GameScreen.borders.length && GameScreen.borders[borderInFrontIndex].side == 0)) {
                                    stopTurnEngine(Constants.SHIP_TURNSTATE_FREE);
                                    npcMoveState = MOVE_FREE_MOVE;
                                }
                                else if (frontDistance < MIN_DISTANCE) {
                                    startLeftEngine();
                                }
                                else {
                                    startRightEngine(turnMinAngle);
                                    increaseMainEnginePower();
                                }
                            break;

                            case MOVE_TURN_RIGHT:
                                if ((borderInFrontIndex > -1 && borderInFrontIndex < GameScreen.borders.length && GameScreen.borders[borderInFrontIndex].side == 1)) {
                                    stopTurnEngine(Constants.SHIP_TURNSTATE_FREE);
                                    npcMoveState = MOVE_FREE_MOVE;
                                }
                                else if (frontDistance < MIN_DISTANCE) {
                                    startRightEngine();
                                }
                                else {
                                    startRightEngine(turnMaxAngle);
                                    increaseMainEnginePower();
                                }
                            break;

                            case MOVE_BREAK:
                                npcMoveState = MOVE_STOP;
                            break;
                        }
                    break;

                    /*** OBSTACLE_WALL **************************************************************************/
                    case Constants.OBSTACLE_WALL:
                        switch (npcMoveState) {
                            case MOVE_STOP:
                                increaseMainEnginePower();
                            break;

                            case MOVE_FREE_MOVE:
                            case MOVE_TURN_LEFT:
                            case MOVE_TURN_RIGHT:
                                if ((borderInFrontIndex > -1) && (borderInFrontIndex < GameScreen.borders.length)) {
                                    if (wpDirection == Constants.DIRECTION_LEFT) {
                                        if (locationAlienShip == Constants.LOCATION_MIDDLE_LEFT || leftDistance < CRITICAL_DISTANCE) {
                                            if (locationAlienShip == Constants.LOCATION_MIDDLE_RIGHT || rightDistance < CRITICAL_DISTANCE) {
                                                startBrakeEngine();
                                                npcMoveState = MOVE_BREAK;
                                            }
                                            else {
                                                startRightEngine();
                                                npcMoveState = MOVE_TURN_RIGHT;
                                            }
                                        }
                                        else {
                                            startLeftEngine();
                                            npcMoveState = MOVE_TURN_LEFT;
                                        }
                                    }
                                    else {
                                        if (locationAlienShip == Constants.LOCATION_MIDDLE_RIGHT || rightDistance < CRITICAL_DISTANCE) {
                                            if (locationAlienShip == Constants.LOCATION_MIDDLE_LEFT || leftDistance < CRITICAL_DISTANCE) {
                                                startBrakeEngine();
                                                npcMoveState = MOVE_BREAK;
                                            }
                                            else {
                                                startLeftEngine();
                                                npcMoveState = MOVE_TURN_LEFT;
                                            }
                                        }
                                        else {
                                            startRightEngine();
                                            npcMoveState = MOVE_TURN_RIGHT;
                                        }
                                    }
                                }

                                if (frontDistance < MIN_DISTANCE) {
                                    startBrakeEngine();
                                }
                                if (stepX == 0 && stepY == 0) {
                                    npcMoveState = MOVE_STOP;
                                }
                            break;

                            case MOVE_BREAK:
                                if (stepX == 0 && stepY == 0) {
                                    npcMoveState = MOVE_STOP;
                                }
                            break;
                        }
                    break;
                }
                
                if ((!isTargetSet) && (posY >= nwpPosY) && (npcMoveState != MOVE_FINISHED)) {
                    // Next waypoint is reached
                    curWayPoint = nextWayPoint;
                    nextWayPoint++;
                    if (nextWayPoint < GameScreen.wayPoints[curWay].length) {
                        npcGetNextWayPoint();
                    }
                    else {
                        npcMoveState = MOVE_FINISHED;
                    }
                }

                if (posY >= GameScreen.trackLength) {
                    racingTime = System.currentTimeMillis() - GameScreen.timeStartGame;
                    shipState = Constants.SHIP_STATE_FINISH;
                    aBody.startAnimation(Constants.SHIP_FREE_ANIMATIONS[type][bodyState], Constants.ANIMATION_PLAY_REPEAT);
                }
            break;
            
            case Constants.SHIP_STATE_FINISH:
                if (leftPosY > GameScreen.trackLength) {
                    if (V > 0) {
                        stopTurnEngine(Constants.SHIP_TURNSTATE_FREE);
                        startBrakeEngine(getBrakeForce((Constants.FL_COUNTTILES * Constants.TILE_HEIGHT) << 1));
                    }
                    shipState = Constants.SHIP_STATE_STOP;
                }
                else if (V == 0) shipState = Constants.SHIP_STATE_STOP;
            break;
        }
    }
}